<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="text" id="i1">
<input type="button" value="平方" onclick="f()">
<div>结果:<span>0</span></div>
<script>
    function f() {
        let i = document.querySelector("#i1");
        let s = document.querySelector("span");

        //isNaN()判断变量是否是NaN
        if (isNaN(i.value)) {
            s.innerText = "输入错误!";
            return;
        }
        //假如做其他运算,当两个字符串进行-*/的运算时会自动转换为数值,但是进行+运算时是拼接
        //s.innerText = i.value*1 + i.value*1; 第一种,强制转换为数值
        //第二种 parseFloat() 将字符串转成整数或小数
        s.innerText = parseFloat(i.value) + parseFloat(i.value);
    }
</script>
</body>
</html>